﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:systab
	/// </summary>
	public partial class systab
	{
		public systab()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("pkid", "systab"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int pkid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from systab");
			strSql.Append(" where pkid=@pkid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@pkid", MySqlDbType.Int32)
			};
			parameters[0].Value = pkid;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.systab model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into systab(");
			strSql.Append("tabid,indexid,name,description,url,backup1,isroot,fshowimg)");
			strSql.Append(" values (");
			strSql.Append("@tabid,@indexid,@name,@description,@url,@backup1,@isroot,@fshowimg)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@tabid", MySqlDbType.VarChar,4),
					new MySqlParameter("@indexid", MySqlDbType.VarChar,2),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@description", MySqlDbType.VarChar,255),
					new MySqlParameter("@url", MySqlDbType.VarChar,100),
					new MySqlParameter("@backup1", MySqlDbType.VarChar,50),
					new MySqlParameter("@isroot", MySqlDbType.VarChar,50),
					new MySqlParameter("@fshowimg", MySqlDbType.VarChar,50)};
			parameters[0].Value = model.tabid;
			parameters[1].Value = model.indexid;
			parameters[2].Value = model.name;
			parameters[3].Value = model.description;
			parameters[4].Value = model.url;
			parameters[5].Value = model.backup1;
			parameters[6].Value = model.isroot;
			parameters[7].Value = model.fshowimg;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.systab model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update systab set ");
			strSql.Append("tabid=@tabid,");
			strSql.Append("indexid=@indexid,");
			strSql.Append("name=@name,");
			strSql.Append("description=@description,");
			strSql.Append("url=@url,");
			strSql.Append("backup1=@backup1,");
			strSql.Append("isroot=@isroot,");
			strSql.Append("fshowimg=@fshowimg");
			strSql.Append(" where pkid=@pkid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@tabid", MySqlDbType.VarChar,4),
					new MySqlParameter("@indexid", MySqlDbType.VarChar,2),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@description", MySqlDbType.VarChar,255),
					new MySqlParameter("@url", MySqlDbType.VarChar,100),
					new MySqlParameter("@backup1", MySqlDbType.VarChar,50),
					new MySqlParameter("@isroot", MySqlDbType.VarChar,50),
					new MySqlParameter("@fshowimg", MySqlDbType.VarChar,50),
					new MySqlParameter("@pkid", MySqlDbType.Int32,11)};
			parameters[0].Value = model.tabid;
			parameters[1].Value = model.indexid;
			parameters[2].Value = model.name;
			parameters[3].Value = model.description;
			parameters[4].Value = model.url;
			parameters[5].Value = model.backup1;
			parameters[6].Value = model.isroot;
			parameters[7].Value = model.fshowimg;
			parameters[8].Value = model.pkid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int pkid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from systab ");
			strSql.Append(" where pkid=@pkid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@pkid", MySqlDbType.Int32)
			};
			parameters[0].Value = pkid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string pkidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from systab ");
			strSql.Append(" where pkid in ("+pkidlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.systab GetModel(int pkid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select pkid,tabid,indexid,name,description,url,backup1,isroot,fshowimg from systab ");
			strSql.Append(" where pkid=@pkid");
			MySqlParameter[] parameters = {
					new MySqlParameter("@pkid", MySqlDbType.Int32)
			};
			parameters[0].Value = pkid;

			hcrainMvc.Model.systab model=new hcrainMvc.Model.systab();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.systab DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.systab model=new hcrainMvc.Model.systab();
			if (row != null)
			{
				if(row["pkid"]!=null && row["pkid"].ToString()!="")
				{
					model.pkid=int.Parse(row["pkid"].ToString());
				}
				if(row["tabid"]!=null)
				{
					model.tabid=row["tabid"].ToString();
				}
				if(row["indexid"]!=null)
				{
					model.indexid=row["indexid"].ToString();
				}
				if(row["name"]!=null)
				{
					model.name=row["name"].ToString();
				}
				if(row["description"]!=null)
				{
					model.description=row["description"].ToString();
				}
				if(row["url"]!=null)
				{
					model.url=row["url"].ToString();
				}
				if(row["backup1"]!=null)
				{
					model.backup1=row["backup1"].ToString();
				}
				if(row["isroot"]!=null)
				{
					model.isroot=row["isroot"].ToString();
				}
				if(row["fshowimg"]!=null)
				{
					model.fshowimg=row["fshowimg"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select pkid,tabid,indexid,name,description,url,backup1,isroot,fshowimg ");
			strSql.Append(" FROM systab ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM systab ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.pkid desc");
			}
			strSql.Append(")AS Row, T.*  from systab T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "systab";
			parameters[1].Value = "pkid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

